<template>

  <div>

    <el-dialog title="新增任务" :visible.sync="batchTaskflag">
      <el-form :model="batchTask"  refs="dbForm" >





        <el-form-item label="源数据库" :label-width="formLabelWidth">
          <selectTool :options="dbs" label="dbname"  ref="selectTool1"  @selectedindex="getSrcDbid"></selectTool>
        </el-form-item>

        <el-form-item label="目标数据库" :label-width="formLabelWidth">
          <selectTool :options="dbs" label="dbname"   ref="selectTool2"  @selectedindex="getDestDbid"></selectTool>
        </el-form-item>

        <el-form-item label="变量值设置" :label-width="formLabelWidth">
          <ul v-for="i in batchTask.variables">
            <li> 用例名：{{i.key}}--变量名：{{i.value.varName}}: <el-input v-model="i.value.value" style="width: 75%"></el-input></li>

          </ul>

        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">

        <el-button @click="batchTaskflag = false">取 消</el-button>

        <el-button type="primary" @click="submitBatchTask">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
  import selectTool from '../../shuiyoutools/components/selectTool'
  import {saveTasks} from '../../../api/shuzhiCompare'
    export default {
        name: "batchTaskCretae",
      data(){
          return{
            formLabelWidth:'150px',
            batchTaskflag:false,
            batchTask:{
              variables:[],
               srcDb:{},
              destDb:{},
            },


            newtasks:[],
            newTaskVariable:{
              id:0,
              varName:'',
              value:'',
              taskId:0
            }
          }
      },
      components:{
        selectTool
      },
      methods:{

        getSrcDbid(index){
          this.batchTask.srcDb = this.dbs[index]

        },
        getDestDbid(index){
          this.batchTask.destDb = this.dbs[index]
        },


        initBatchTask(){
          console.log('initBatchTask')
          //初始化
          this.newtasks = []
          this.batchTask={
            variables:[],
              srcDb:{},
            destDb:{},
          }
            for(let i in this.scripts){
              let scriptdto  = this.scripts[i]
              let newtask = {
                id:0,
                name:'',
                project:{},
                script:{},
                variables:[],
                srcDb:{},
                destDb:{},
                autoflag:0,
                status:1
              }


              for (let i in scriptdto.variables){
                let newTaskVariable={
                  id:0,
                  varName:'',
                  value:'',
                  taskId:0
                }

                newTaskVariable.varName = scriptdto.variables[i].name
                console.log('taskdto variables')
                console.log(i)
                console.log(newTaskVariable)
                newtask.variables.push(newTaskVariable)
                this.batchTask.variables.push({key:scriptdto.name,value:newTaskVariable})

              }
              newtask.script=scriptdto
              this.newtasks.push(newtask)

            }
          console.log(this.batchTask)
          console.log(this.newtasks)

          },

        submitBatchTask(){
          let that = this
            let srcdb = this.batchTask.srcDb
          let destDb = this.batchTask.destDb


            for(let i in this.newtasks){
              this.newtasks[i].srcDb = srcdb
              this.newtasks[i].destDb = destDb
            }

          saveTasks(this.newtasks).then(res=>{
            that.batchTaskflag = false
          }).catch(err=>{
            that.batchTaskflag = false
          })




        },

          init(){
            this.$refs.selectTool2.value = ''
            this.$refs.selectTool1.value = ''

          },




      },

      mounted(){
          this.init(),
        this.initBatchTask()
      },
      props:[
        'dbs',
        'scripts',


      ]
    }
</script>

<style scoped>

</style>
